package bestTeam.WalkieDoggie.client.Composites;

import org.cobogw.gwt.user.client.ui.Rating;

import bestTeam.WalkieDoggie.client.WalkieDoggie;
import bestTeam.WalkieDoggie.client.Serializables.UserProfileData;
import bestTeam.WalkieDoggie.client.Serializables.UserRequest;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HistoryRequest extends Composite{

	private VerticalPanel panel;
	private Rating rate;

	private RadioButton veryNotRecommended;
	private RadioButton isOKButWillNotTakeThisWalkerAgain;
	private RadioButton thisWalkerIsFine;
	private RadioButton isaGreatWalker;
	private RadioButton stronglyRecomended;

	public HistoryRequest(final UserRequest request){

		panel = new VerticalPanel();

		buildHistoryRequestComposite(request);

		initWidget(panel);
	}

	private void buildHistoryRequestComposite(final UserRequest request) {
		panel.clear();
		Label labelWalker = new Label();
		panel.add(labelWalker);
		if(request.getApprovedWalkerID()!=null && !request.equals("")){

			UserProfileData user = HistoryComposite.myWalkersMap.get(request.getApprovedWalkerID());
			if(user!=null)
				labelWalker.setText("Your walker was: "+user.getNickName());

			else				
				labelWalker.setText("there was a problem loading the walker details of this request");

			// its ranked
			Label labelrank = new Label();
			panel.add(labelrank);

			if(request.getRank()!=-1){
				labelrank.setText("Your rank for this request: ");
				rate = new Rating((int)request.getRank(), 5, Rating.LTR, "iBonesmall.png","iBoneBWsmall.png",
						"iBonesmall.png", 16, 28);
				rate.setReadOnly(true);
				panel.add(rate);
			}

			//its not ranked
			else{
				labelrank.setText("Please rank this request:");
				rate = new Rating(0, 5, Rating.LTR, "iBonesmall.png","iBoneBWsmall.png","iBonesmall.png", 22, 24);
				rate.setTitles(new String[]{"veryNotRecommended","isOKButWillNotTakeThisWalkerAgain","thisWalkerIsFine","isaGreatWalker","stronglyRecomended"});
				panel.add(rate);
//				Label selected = new Label("selected: " + getRank());
//				panel.add(selected);

//				veryNotRecommended= new RadioButton("ranking");
//				veryNotRecommended.setText("I would Strongly recommend that you will not use this walker");
//				isOKButWillNotTakeThisWalkerAgain = new RadioButton("ranking");
//				isOKButWillNotTakeThisWalkerAgain.setText("This walker is OK, But i will not use him/her again");
//				thisWalkerIsFine = new RadioButton("ranking");
//				thisWalkerIsFine.setText("This walker is fine");
//				isaGreatWalker = new RadioButton("ranking");
//				isaGreatWalker.setText("This walker is a great walker");
//				stronglyRecomended = new RadioButton("ranking");
//				stronglyRecomended.setText("I strongly reccomend this walker");
//
//				panel.add(veryNotRecommended);
//				panel.add(isOKButWillNotTakeThisWalkerAgain);
//				panel.add(thisWalkerIsFine);
//				panel.add(isaGreatWalker);
//				panel.add(stronglyRecomended);
//
				Button rankButton = new Button("Rank");
				rankButton.addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {

						request.setRank(getRank());
						WalkieDoggie.requestService.UpdateRequest(request, new AsyncCallback<Void>() {

							@Override
							public void onSuccess(Void result) {
								WalkieDoggie.usersService.setRanking(request.getApprovedWalkerID(), request.getRank(), new AsyncCallback<Double>() {

									@Override
									public void onSuccess(Double currentRanking) {

										UserProfileData user = WalkieDoggie.requestsOwnersMap.get(request);
										user.setRanking(currentRanking);

										buildHistoryRequestComposite(request);

									}

									@Override
									public void onFailure(Throwable caught) {
										WalkieDoggie.handleError(caught);

									}
								});

							}

							@Override
							public void onFailure(Throwable caught) {
								WalkieDoggie.handleError(caught);	
							}
						});

					}

				});

				panel.add(rankButton);
			}
		}
		else
			labelWalker.setText("You did not have a walker for this request");

	}

	private void updateHistoryRequest(UserRequest request) {

		panel.clear();


	}

	private double getRank() {

		return rate.getRating();


	}
}
